package com.huluobo.a2109mvpimdemo.register;


import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.huluobo.a2109mvpimdemo.R;
import com.huluobo.a2109mvpimdemo.base.BaseActivity;

import org.w3c.dom.Text;

import java.util.Random;

public class RegisterActivity extends BaseActivity<RegisterContract.View, RegisterPresenter> implements RegisterContract.View {

    private EditText username, password, rePassword;

    private Button register_btn, timeBtn;
    private CountDownTimer countDownTimer;

    @Override
    protected void initData() {
        presenter = new RegisterPresenter();
    }

    @Override
    protected void initView() {
        TextView title = findViewById(R.id.header_title);
        title.setText("注册");
        ImageView back = findViewById(R.id.header_back);
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(v -> finish());
        username = findViewById(R.id.register_username);
        password = findViewById(R.id.register_password);
        rePassword = findViewById(R.id.register_re_password);
        register_btn = findViewById(R.id.register_btn);
        timeBtn = findViewById(R.id.timeButton);

        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //当倒计时的时候
                timeBtn.setEnabled(false);//设置获取按钮不可点
                timeBtn.setText(millisUntilFinished / 1000 + "秒");
            }

            @Override
            public void onFinish() {
                //当倒计时完成的时候
                timeBtn.setText("重新获取");
                timeBtn.setEnabled(true);
            }
        };

        timeBtn.setOnClickListener(v -> {
            countDownTimer.start();
            Random random = new Random();
            int i = random.nextInt(999999);
            Toast.makeText(this, "验证码:" + i, Toast.LENGTH_SHORT).show();
        });

        register_btn.setOnClickListener(v -> {
            presenter.register(username.getText().toString(), password.getText().toString(),
                    rePassword.getText().toString());
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        countDownTimer.cancel();
    }

    @Override
    protected int bindingLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void onRegisterSuccess() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dismissProgressDialog();
                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }

    @Override
    public void onRegisterFailed(String error) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dismissProgressDialog();
                Toast.makeText(RegisterActivity.this, "注册失败:" + error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onStartRegister() {
        showProgressDialog("正在注册...");
    }

    @Override
    public void onUsernameError() {
        username.setError("用户名输入错误,需要首字符为英文,3-20位");
    }

    @Override
    public void onPasswordError() {
        password.setError("密码输入错误,需要3-20位数字");
    }

    @Override
    public void onRePasswordError() {
        rePassword.setError("2次密码输入不一致");
    }
}